/**********************************************************************************
**
** Copyright (C) 2009 Jostein Bratlie
** Contact: jostein.bratlie@gmail.com | http://bratlie.info | http://episteme.hin.no
**
** This file is part of GMQtExt, a GMlib and Qt extension library
**
** GMQtExt is free software: you can redistribute it and/or modify
** it under the terms of the GNU Lesser General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** GMQtExt is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public License
** along with GMQtExt.  If not, see <http://www.gnu.org/licenses/>.
**
**********************************************************************************/

#include <gmOpenGL>

// Qt
#include <QtGui>
#include <QGLWidget>

// Local
#include "graphicsscene.h"

// Header
#include "graphicsview.h"


namespace GMQtExt {

  GraphicsView::GraphicsView( QWidget* parent, bool run_init ) : QGraphicsView( parent ) {

    _scene = 0x0;

    if( run_init )
      init();
  }


  void GraphicsView::init() {

    initViewport();

    initScene();
  }


  void GraphicsView::initScene() {

    setScene( new GraphicsScene() );
  }


  void GraphicsView::initViewport() {

    setViewport( new QGLWidget(
      QGLFormat(
        QGL::AlphaChannel |
        QGL::DoubleBuffer |
        QGL::DepthBuffer |
        QGL::Rgba |
        QGL::SampleBuffers |
        QGL::StereoBuffers
      )
    ) );
  }


  void GraphicsView::setScene( GraphicsScene* scene ) {

    QGLWidget *widget =dynamic_cast<QGLWidget*>( viewport() );
    if( !widget )
      initViewport();

    _scene = scene;
    QGraphicsView::setScene( scene );
  }


  void GraphicsView::resizeEvent ( QResizeEvent* e ) {

    if( _scene )
      _scene->resize( QRect(QPoint(0, 0), e->size()) );

    QGraphicsView::resizeEvent(e);
  }

} // END namespace GMQtExt



